/**
 * Created with IntelliJ IDEA.
 * User: zzl
 * Date: 13-1-4
 * To change this template use File | Settings | File Templates.
 */
(function ($, undefined) {

    var Toast = function (elem, options) {
        this.$elem = $(elem);
        this.options = options;
        this.setup();
    };

    var proto = { constructor: Toast };
    Toast.prototype = proto;

    proto.setup = function () {
        var me = this;
        me.$elem.css({
            position: 'fixed',
            display: 'none'
        });
    };

    proto.show = function () {
        var me = this;
        var width = me.$elem.outerWidth();
        var height = me.$elem.outerHeight();
        var viewport = domex.getViewport();
        var margin = me.options.margin;
        me.$elem.css({
            left: viewport.x2 - viewport.x1 - width - margin,
            top: viewport.y2 - viewport.y1 - height - margin
        });
        clearTimeout(me.hideTimer);
//        me.$elem.hide();
        me.$elem.fadeIn(me.options.inDuration, function () {
            me.hideTimer = setTimeout(function () {
                me.hide();
            }, me.options.showDuration);
        });

    };

    proto.hide = function () {
        var me = this;
        me.$elem.fadeOut(me.options.outDuration);
    };

    $.fn.toast = $.addPluginFn('toast', Toast, {
        margin: 3,
        inDuration: 200,
        showDuration: 600,
        outDuration: 200
    });

})(jQuery);